package com.lanou.dllo.findfood.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.findfood.activity.AdviceActivtity;
import com.lanou.dllo.findfood.activity.ApplicationActivity;
import com.lanou.dllo.findfood.activity.DiaryFristActvity;
import com.lanou.dllo.findfood.activity.MyCenterActivity;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.activity.LoginAvtivtiy;
import com.lanou.dllo.findfood.tools.UpdateManger;
import com.lanou.dllo.findfood.util.CircleImage;


/**
 * Created by dllo on 15/10/20.
 */
public class CenterFragment extends Fragment implements View.OnClickListener {
    private TextView myDarTv, center, personcenter, statu,
            personName, advice, userName, newVersion, go_login, setRecommend,helpe;
    private ImageView goBack;
    private CircleImage btn_person;
    private String name, status, qqName;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, null);
        return view;
    }

    //定义一个接口
    public interface FOneBtnClickListener {
        void onFOneBtnClick();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        //登录界面activity向fragment中传值
        getLogName();
        //  getqqName();
    }

    public void getLogName() {
        Intent intent = getActivity().getIntent();
        name = intent.getStringExtra("UserName");// 得到键值put对应的get
        status = intent.getStringExtra("isLogin");
        go_login.setText(status);
        userName.setText(name);//得到从登录界面传来的用户名

    }

    public void getqqName() {
        Intent intent = getActivity().getIntent();
        qqName = intent.getStringExtra("qqName");
        userName.setText(qqName);
    }

    private void initView() {
        myDarTv = (TextView) getActivity().findViewById(R.id.myDarTv);
        myDarTv.setOnClickListener(this);
        personcenter = (TextView) getActivity().findViewById(R.id.personcenter);
        personcenter.setOnClickListener(this);
        btn_person = (CircleImage) getActivity().findViewById(R.id.btn_person);
        btn_person.setOnClickListener(this);
        userName = (TextView) getActivity().findViewById(R.id.userName);
        advice = (TextView) getActivity().findViewById(R.id.advice);
        advice.setOnClickListener(this);

        go_login = (TextView) getActivity().findViewById(R.id.go_login);
        helpe = (TextView) getActivity().findViewById(R.id.helper);
        helpe.setOnClickListener(this);

        TextView recommendApp = (TextView) getActivity().findViewById(R.id.recommendApp);
        recommendApp.setOnClickListener(this);
    }

    private UpdateManger mUpdateManager;


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.helper:
                String number = "400-651-7919";
                Intent intent_call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                startActivity(intent_call);
                break;
            case R.id.recommendApp://应用推荐
                startActivity(new Intent(getActivity(), ApplicationActivity.class));
                break;
            case R.id.myDarTv://我的菜谱
                Intent intent = new Intent(getActivity(), DiaryFristActvity.class);
                startActivityForResult(intent, 100);
                break;
            case R.id.personcenter://个人中心
                Intent center_inte = new Intent(getActivity(), MyCenterActivity.class);
                center_inte.putExtra("Name", name);// 相当于是键值 将从登录界面传来的值传给个人中心
                startActivityForResult(center_inte, 500);
                break;

            case R.id.advice://意见反馈
                startActivity(new Intent(getActivity(), AdviceActivtity.class));
                break;

            case R.id.btn_person://切换用户
                showAlerterDialog();
                break;
            default:
                break;
        }
        //交给宿主Acticity
        if (getActivity() instanceof FOneBtnClickListener) {
            ((FOneBtnClickListener) getActivity()).onFOneBtnClick();
        }
    }

    private void showAlerterDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("登录");
        builder.setIcon(R.mipmap.app_icon_longin_user);
        if (userName.length() == 0) {

            builder.setMessage("您还没有登录,现在去登录");
        } else {
            builder.setMessage("切换用户");

        }

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent1 = new Intent(getActivity(), LoginAvtivtiy.class);
                startActivity(intent1);
                userName.setText("");

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //  Toast.makeText(getActivity(),"您取消了本次登录",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 500 && resultCode == 500) {
            userName.setText(data.getStringExtra("getName"));//得到从个人中心activity传来的值
            Bitmap bitmap = data.getParcelableExtra("personImage");
            btn_person.setImageBitmap(bitmap);

        }
    }
}
